package com.china08.yxyapi.api.schwebsite.navbar;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSection;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSectionRepository;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;

/**
 * 
 * 提取一级栏目信息
 */
@CrossOrigin
@RestController("SwsNavBarTopSectionListController")
public class SwsNavBarTopSectionListController extends YxyApiBaseController {

    @Autowired
    private SwsSettingDomainsRepository swsSettingDomainsRepository;

    @Autowired
    private SwsSectionRepository swsSectionRepository;

    @RequestMapping(value = "/api_yw/schwebsite/navbar/top/sections", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String domainName = getDomainName(request);
        String pid = "0";

        Assert.isTrue(StringUtils.isNotBlank(domainName), "域名为空");
        // 通过域名获取学校ID
        SwsSettingDomains DomainName = swsSettingDomainsRepository.findByDomainName(domainName);
        Assert.isTrue(null != DomainName, "域名对象为空");
        String schoolId = DomainName.getSchoolId();
        boolean isVisible = true;
        boolean showNavigation = true;
        List<SwsSection> bySchoolIdAndPid = swsSectionRepository
                .findBySchoolIdAndIsVisibleAndShowNavigationAndPidOrderByOrderNumAsc(schoolId, isVisible,
                        showNavigation, pid);

        Assert.isTrue(null != bySchoolIdAndPid, "栏目对象为空");
        // 获取二级栏目ID
        List<RespModel> list = new ArrayList<RespModel>();

        for (SwsSection swsSection : bySchoolIdAndPid) {
            String id = swsSection.getId();
            String sectionName = swsSection.getSectionName();
            boolean isOutLink = swsSection.getIsOutLink();
            String outLink = swsSection.getOutLink();
            Integer sectionType = swsSection.getSectionType();
            String section1Id = swsSection.getId();
            List<SwsSection> sectionList = swsSectionRepository
                    .findBySchoolIdAndIsVisibleAndShowNavigationAndPidOrderByOrderNumAsc(schoolId, isVisible,
                            showNavigation, section1Id);

            List<SectionChild> sectionList2 = new ArrayList<>();
            for (SwsSection swsSection2 : sectionList) {
                String id2 = swsSection2.getId();
                String sectionName2 = swsSection2.getSectionName();
                boolean isOutLink2 = swsSection2.getIsOutLink();
                Integer sectionType2 = swsSection2.getSectionType();
                String outLink2 = swsSection2.getOutLink();
                String section2Id = swsSection2.getId();
                SectionChild sectionChild = new SectionChild();
                sectionChild.id = id2;
                sectionChild.sectionName = sectionName2;
                sectionChild.isOutLink = isOutLink2;
                sectionChild.outLink = outLink2;
                sectionChild.section2Id = section2Id;
                sectionChild.sectionType = sectionType2;
                sectionList2.add(sectionChild);
            }

            RespModel respModel = new RespModel();
            respModel.id = id;
            respModel.sectionName = sectionName;
            respModel.isOutLink = isOutLink;
            respModel.outLink = outLink;
            respModel.section2List = sectionList2;
            respModel.section1Id = section1Id;
            respModel.sectionType = sectionType;
            list.add(respModel);
        }

        return list;
    }

    protected static class ReqModel extends YxyApiReqModel {

    }

    protected static class SectionChild {

        public String id;

        public boolean isOutLink;

        public String outLink;

        public String sectionName;

        public String section2Id;

        public Integer sectionType;
    }

    protected static class RespModel extends YxyApiRespModel {

        public String id;

        public String section1Id;

        public boolean isOutLink;

        public String outLink;

        public String sectionName;

        public Integer sectionType;

        public List<SectionChild> section2List;
    }
}
